package com.uade.magicCompany.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.uade.magicCompany.entities.RapidPassEntity;
import com.uade.magicCompany.model.RapidPass;

public class DAORapidPassImpl extends
		AbstractBaseGenericDAOImpl<RapidPassEntity, Long> implements
		DAORapidPass {

	@Override
	public ArrayList<RapidPassEntity> buscarRapidPassPorAtracciones(
			List<Integer> idAtracciones) {
		// Get the entity manager for the tests.
		emf = Persistence.createEntityManagerFactory("JEE6Demo-Persistence");
		em = emf.createEntityManager();
		try {
			// Get a new transaction
			EntityTransaction trx = em.getTransaction();

			// Start the transaction
			trx.begin();
			// Get list
			List<RapidPassEntity> rapidPassEntities = new ArrayList<RapidPassEntity>();
			for (Integer idAtraccion : idAtracciones) {
				Query query = em
						.createQuery(
								"SELECT c FROM RapidPassEntity c WHERE c.atraccion.id = :atraccionId")
						.setParameter("atraccionId", new Long(idAtraccion));
				List<RapidPass> rapidpasses=query.getResultList();
				for (int i=0;i<rapidpasses.size();i++){
					rapidPassEntities.add((RapidPassEntity) query.getResultList()
							.get(i));
				}
			}
			return (ArrayList<RapidPassEntity>) rapidPassEntities;

		} catch (RuntimeException e) {
			// if (trx != null && trx.isActive()) {
			// trx.rollback();
			// }
			throw e;
		} finally {
			// Close the manager
			em.close();

		}
		

	}

	@Override
	public List<RapidPassEntity> buscarRapidPassPorEntrada(Long id) {
		// Get the entity manager for the tests.
		emf = Persistence.createEntityManagerFactory("JEE6Demo-Persistence");
		em = emf.createEntityManager();
		try {
			// Get a new transaction
			EntityTransaction trx = em.getTransaction();

			// Start the transaction
			trx.begin();
			// Get list
			Query query = em
					.createQuery(
							"SELECT c FROM RapidPassEntity c WHERE c.entrada.id = :idEntrada")
					.setParameter("idEntrada", id);
			List<RapidPassEntity> rapidPasses = (List<RapidPassEntity>) query
					.getResultList();
			// Commit and end the transaction
			trx.commit();
			return rapidPasses;

		} catch (RuntimeException e) {
			// if (trx != null && trx.isActive()) {
			// trx.rollback();
			// }
			throw e;
		} finally {
			// Close the manager
			em.close();

		}
	}

}
